/*
 * $Id: OnWatchLogger.java,v 1.3 2009/07/10 21:31:37 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.util;

import org.springframework.util.Assert;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.database.dao.ITaskDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActOnwatchTask;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgCodeDetails;
import com.ge.healthcare.autosc.common.dbLogger.StepLogger;
import com.ge.healthcare.autosc.common.message.converter.AutoSCMessage;
/**
 * Wrapper for StepLogger.
 * @author 212042946
 *
 */
public class OnWatchLogger {

	private static ITaskDAO taskDAO = null;
	private static ICodeDetailsDAO codeDetailsDAO = null;
	
	public void init() {
		Assert.notNull(taskDAO,
			"ActTaskDAO is not defined. Use setActTaskDAO to provide one");
		Assert.notNull(codeDetailsDAO,
			"CodeDetailsDAO is not defined. Use setCodeDetailsDAO to provide one");
	}
	
	public static void startLogProcessing(ActOnwatchTask onwatchTask) {
		if(onwatchTask != null) {
			ActTask task = taskDAO.getTaskByID(onwatchTask.getTaskId());
			if(task != null) {
				StepLogger.logStartTime(task.getTaskId(), StepLogger.TASK_TYPE);
			}
		}
	}
	
	public static void startLogProcessing(AutoSCMessage message) {
		if(message != null) {
			StepLogger.logStartTime(message.getObjectId(), message.getObjectType());
		}
	}
	
	public static void addStepLog(String log) {
		StepLogger.addStepLog(log);
	}
	
	public static void endLogProcessing(String codeName, String defaultCode) {
		if(codeName == null) {
			codeName = defaultCode;
		}
		endLogProcessing(codeName);
	}

	public static void endLogProcessing(String codeName) {
		CfgCodeDetails codeDetails = codeDetailsDAO.getUniqueCodedetailsByCodeName(codeName);
		if(codeDetails != null) {
			StepLogger.logEndTime(codeDetails.getCodeId());
		} else {
			ASCLogger.error(OnWatchLogger.class, "endLogProcessing", 
					"Cannot cerate stepLog. Code details not found for codeName: " + codeName);
		}
	}
	
	public void setActTaskDAO(ITaskDAO taskDAO) {
		OnWatchLogger.taskDAO = taskDAO;
	}
	
	public void setCodeDetailsDAO(ICodeDetailsDAO codeDetailsDAO) {
		OnWatchLogger.codeDetailsDAO = codeDetailsDAO;
	}
}
